<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/_init_model.php';
	
	class mod_p_rate_result extends _init_model
	{		
		public $P_RATE_RT_ID;
		public $P_RATE_RT_SCORE_0;
		public $P_RATE_RT_SCORE_1;
		public $P_RATE_RT_SCORE_2;
		public $P_RATE_RT_SCORE_3;
		public $P_RATE_RT_SCORE_4;
		public $P_RATE_RT_SCORE_5;
		public $P_RATE_RT_FREQ;
		public $CAF_PDT_ID;
		public $CAF_ID;
			
		function __construct()
		{
			parent::__construct();
				
			$this->tableName = 'p_rate_result';
		}
		
		function update_freq($new_score, $old_score=null, $product_id, $cafe_id)
		{
			$obj = new $this;
			$result = $obj->get(array('CAF_PDT_ID' => $product_id,										
										'CAF_ID' => $cafe_id));

			if ($result->count() > 0)	
			{	
				$current_freq = 0;
					
				// decrease old freq
				if (!is_null($old_score))
				{			print ' step = 2';		
					$sqlcmd_2 = 'UPDATE ' . $this->tableName . ' SET';
					
					switch ($old_score)
					{
						case 0: case '0':
							$current_freq = ($result[0]->P_RATE_RT_SCORE_0 - 1);
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_0 = ' . ($result[0]->P_RATE_RT_SCORE_0 - 1);
							break;
						case 1: case '1':
							$current_freq = ($result[0]->P_RATE_RT_SCORE_1 - 1);
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_1 = ' . ($result[0]->P_RATE_RT_SCORE_1 - 1);
							break;
						case 2: case '2':		
							$current_freq = ($result[0]->P_RATE_RT_SCORE_2 - 1);	
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_2 = ' . ($result[0]->P_RATE_RT_SCORE_2 - 1);
							break;
						case 3: case '3':	
							$current_freq = ($result[0]->P_RATE_RT_SCORE_3 - 1);				
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_3 = ' . ($result[0]->P_RATE_RT_SCORE_3 - 1);
							break;
						case 4: case '4':
							$current_freq = ($result[0]->P_RATE_RT_SCORE_4 - 1);
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_4 = ' . ($result[0]->P_RATE_RT_SCORE_4 - 1);
							break;
						case 5: case '5':		
							$current_freq = ($result[0]->P_RATE_RT_SCORE_5 - 1);			
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_5 = ' . ($result[0]->P_RATE_RT_SCORE_5 - 1);
							break;
						case 6: case '6':
							$current_freq = ($result[0]->P_RATE_RT_SCORE_6 - 1);
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_6 = ' . ($result[0]->P_RATE_RT_SCORE_6 - 1);
							break;
						case 7: case '7':
							$current_freq = ($result[0]->P_RATE_RT_SCORE_7 - 1);					
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_7 = ' . ($result[0]->P_RATE_RT_SCORE_7 - 1);
							break;
						case 8: case '8':
							$current_freq = ($result[0]->P_RATE_RT_SCORE_8 - 1);
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_8 = ' . ($result[0]->P_RATE_RT_SCORE_8 - 1);
							break;
						case 9: case '9':			
							$current_freq = ($result[0]->P_RATE_RT_SCORE_9 - 1);		
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_9 = ' . ($result[0]->P_RATE_RT_SCORE_9 - 1);
							break;
						case 10: case '10':
							$current_freq = ($result[0]->P_RATE_RT_SCORE_10 - 1);
							$sqlcmd_2 .= ' P_RATE_RT_SCORE_10 = ' . ($result[0]->P_RATE_RT_SCORE_10 - 1);
							break;
						default:
							return false;
							break;
					}
					
					$this->db->query($sqlcmd_2);
				}				
				
				// increase new freq				
				$sqlcmd = 'UPDATE ' . $this->tableName . ' SET';
								
				switch ($new_score)
				{
					case 0: case '0':
						$sqlcmd .= ' P_RATE_RT_SCORE_0 = ' . ($current_freq + 1);
						break;
					case 1: case '1':
						$sqlcmd .= ' P_RATE_RT_SCORE_1 = ' . ($current_freq + 1);
						break;
					case 2: case '2':			
						$sqlcmd .= ' P_RATE_RT_SCORE_2 = ' . ($current_freq + 1);						
						break;
					case 3: case '3':					
						$sqlcmd .= ' P_RATE_RT_SCORE_3 = ' . ($current_freq + 1);
						break;
					case 4: case '4':
						$sqlcmd .= ' P_RATE_RT_SCORE_4 = ' . ($current_freq + 1);
						break;
					case 5: case '5':					
						$sqlcmd .= ' P_RATE_RT_SCORE_5 = ' . ($current_freq + 1);
						break;
					case 6: case '6':
						$sqlcmd .= ' P_RATE_RT_SCORE_6 = ' . ($current_freq + 1);
						break;
					case 7: case '7':					
						$sqlcmd .= ' P_RATE_RT_SCORE_7 = ' . ($current_freq + 1);
						break;
					case 8: case '8':
						$sqlcmd .= ' P_RATE_RT_SCORE_8 = ' . ($current_freq + 1);
						break;
					case 9: case '9':					
						$sqlcmd .= ' P_RATE_RT_SCORE_9 = ' . ($current_freq + 1);
						break;
					case 10: case '10':
						$sqlcmd .= ' P_RATE_RT_SCORE_10 = ' . ($current_freq + 1);
						break;
					default:
						return false;
						break;
				}				
				
				$sqlcmd .= ' WHERE CAF_PDT_ID = ' . $product_id;
				$sqlcmd .= ' AND CAF_ID = ' . $cafe_id;
							
				return $this->db->query($sqlcmd);
			}
			else
			{
				switch ($new_score)
				{
					case 0: case '0':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_0,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 1: case '1':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_1,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 2: case '2':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_2,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 3: case '3':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_3,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 4: case '4':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_4,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 5: case '5':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_5,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 6: case '6':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_6,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 7: case '7':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_7,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 8: case '8':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_8,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 9: case '9':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_9,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					case 10: case '10':
						$sqlcmd = 'INSERT INTO ' . $this->tableName . '(P_RATE_RT_SCORE_10,CAF_PDT_ID,CAF_ID) VALUES(1,' . $product_id . ',' . $cafe_id. ')';
						return $this->db->query($sqlcmd);
						break;
					default:
						return false;
						break;
				}
			}				
		}	
		function is_rated_product($product_id, $cafe_id)
		{
			$obj = new $this;
			$result = $obj->get(array('CAF_PDT_ID' => $product_id, 'CAF_ID' => $cafe_id), array('P_RATE_RT_ID'));
			
			if ($result->count() > 0)
				return true;
			else
				return false;
		}
		function get_product_result($product_id, $cafe_id)
		{
			$sqlcmd = 'SELECT (((P_RATE_RT_SCORE_1 * 1)+
								(P_RATE_RT_SCORE_2 * 2)+ 
								(P_RATE_RT_SCORE_3 * 3)+
								(P_RATE_RT_SCORE_4 * 4)+
								(P_RATE_RT_SCORE_5 * 5)) / P_RATE_RT_FREQ) AS T_SCORE';
			$sqlcmd .= ' FROM ' . $this->tableName;
			$sqlcmd .= ' WHERE CAF_PDT_ID = ' . $product_id;
			$sqlcmd .= ' AND CAF_ID = ' . $cafe_id;
			
			$query = $this->db->query($sqlcmd);

			if ($query->num_rows() > 0)
				return number_format($query->row()->T_SCORE, 2);
			else
				return '0.00';			
		}		
		function remove_by_product_in($data){			
			$this->db->where_in('CAF_PDT_ID', $data);			
			return $this->db->delete($this->tableName);
		}
		function remove_by_product($product_id){
			$this->db->where('CAF_PDT_ID', $product_id);
			return $this->db->delete($this->tableName);
		}
	}
?>